package com.example.administrator.yanfoxconn.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.administrator.yanfoxconn.R;
import com.example.administrator.yanfoxconn.adapter.AbnormalListAdapter;
import com.example.administrator.yanfoxconn.adapter.DNReformAdapter;
import com.example.administrator.yanfoxconn.bean.AbnormalMessage;
import com.example.administrator.yanfoxconn.bean.JIMessage;
import com.example.administrator.yanfoxconn.bean.YJExceMessage;
import com.example.administrator.yanfoxconn.constant.Constants;
import com.example.administrator.yanfoxconn.constant.FoxContext;
import com.example.administrator.yanfoxconn.utils.BaseActivity;
import com.example.administrator.yanfoxconn.utils.HttpUtils;
import com.example.administrator.yanfoxconn.utils.ToastUtils;
import com.example.administrator.yanfoxconn.widget.SwipeListViewOne;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @Description 营建异常记录列表界面
 * @Author song
 * @Date 7/22/21 7:55 PM
 */
public class YJExceListActivity extends BaseActivity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener{


    private final int MESSAGE_TOAST = 2;
    private final int SUCCESS = 1;
    private final int MESSAGE_SEE_ABNORMAL = 3;

    @BindView(R.id.tv_title)
    TextView tvTitle;//標題
    @BindView(R.id.btn_title_left)
    Button btnBack;//返回
    @BindView(R.id.btn_title_right)
    Button btnAdd;//新增
    @BindView(R.id.ll_is_end)
    LinearLayout llIsEnd;
    @BindView(R.id.rg_end)
    RadioGroup rgEnd;
    @BindView(R.id.rb_all)
    RadioButton rbAll;
    @BindView(R.id.rb_end)
    RadioButton rbEnd;
    @BindView(R.id.lv_abnormal)
    ListView lvAbnormal;//列表
    @BindView(R.id.slv_ji_remark)
    SwipeListViewOne swipeListViewOne;
    @BindView(R.id.rl_time)
    RelativeLayout rlTime;

    private List<YJExceMessage> yjExceMessagesList;
    private DNReformAdapter dnReformAdapter;

    private String result;
    private Boolean isFirst=true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_com_see_abnormal);
        ButterKnife.bind(this);
        tvTitle.setText("異常列表");

        btnBack.setVisibility(View.VISIBLE);
        btnBack.setOnClickListener(this);
        btnAdd.setVisibility(View.VISIBLE);
        btnAdd.setText("新增");
        btnAdd.setOnClickListener(this);
        rlTime.setVisibility(View.GONE);
        llIsEnd.setVisibility(View.VISIBLE);
        rgEnd.setOnCheckedChangeListener(this);
        rbAll.setOnClickListener(this);
        rbEnd.setOnClickListener(this);
        swipeListViewOne.setVisibility(View.VISIBLE);
        lvAbnormal.setVisibility(View.GONE);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_title_left:
                finish();
                break;
            case R.id.btn_title_right:
                Intent intent = new Intent(YJExceListActivity.this,YJExceUpActivity.class);

                startActivity(intent);
                break;
        }
    }

    private void clickSeeOrAdd() {
        dnReformAdapter.setOnClickListenerSeeOrAdd(new DNReformAdapter.OnClickListenerSeeOrAdd() {
            @Override
            public void OnClickListenerSee(int position) {
                Log.e("------", "");
                Intent intent = new Intent(YJExceListActivity.this,YJExceDetailActivity.class);
                if (rbEnd.isChecked()){

                    intent.putExtra("flag","end");
                }else{

                    intent.putExtra("flag","");
                }
                intent.putExtra("id",yjExceMessagesList.get(position).getYj_id());
                startActivity(intent);
            }
        });
    }

    private void getExceList(String isDone) {

        showDialog();
        final String url = Constants.HTTP_YJ_GET_EXCE_LIST+"?yj_area="+FoxContext.getInstance().getType()+"&isDone="+isDone;
        Log.e("-------fff--", "url==" + url);
        new Thread() {
            @Override
            public void run() {
                result = HttpUtils.queryStringForPost(url);

                Gson gson = new Gson();
                Log.e("--fff---result----", result.toString());
                if (result != null) {

                    JsonObject jsonObject = new JsonParser().parse(result).getAsJsonObject();
                    String errCode = jsonObject.get("errCode").getAsString();
                    if (errCode.equals("200")) {

                        JsonArray array = jsonObject.get("data").getAsJsonArray();
                        yjExceMessagesList = new ArrayList<YJExceMessage>();
                        for (JsonElement type : array) {
                            YJExceMessage humi = gson.fromJson(type, YJExceMessage.class);
                            yjExceMessagesList.add(humi);
                        }

                        Message message = new Message();
                        message.what = MESSAGE_SEE_ABNORMAL;
                        message.obj = yjExceMessagesList;
                        mHandler.sendMessage(message);

                    } else if (errCode.equals("400")) {

                        Message message = new Message();
                        message.what = MESSAGE_TOAST;
                        message.obj = "400";
                        mHandler.sendMessage(message);

                    }
                } else {
                    ToastUtils.showShort(YJExceListActivity.this, "請求不成功");
                }
                dismissDialog();
            }
        }.start();
    }
    private void isZg() {

//        showDialog();
        String url = Constants.HTTP_YJ_GET_BASE + "?base_flag=" + "P"+"&base_id="+FoxContext.getInstance().getLoginId();
        new Thread() {
            @Override
            public void run() {
                result = HttpUtils.queryStringForPost(url);

                Log.e("---------", "=getcarLog=fff===" + url);
                Gson gson = new Gson();
                String response = result;

                if (result != null) {
                    Log.e("---------", "result==fff===" + response);
                    JsonObject jsonObject = new JsonParser().parse(response).getAsJsonObject();
                    String errCode = jsonObject.get("errCode").getAsString();
                    if (errCode.equals("200")) {
//                        JsonArray array = jsonObject.get("data").getAsJsonArray();
//                        builds = new ArrayList<YJExceMessage>();
//                        for (JsonElement type : array) {
//                            YJExceMessage humi = gson.fromJson(type, YJExceMessage.class);
//                            builds.add(humi);
//                        }
                        Message message = new Message();
                        message.what = SUCCESS;

                        mHandler.sendMessage(message);
                    } else {
//                        isShowReturn = false;
                        Message message = new Message();
                        message.what = MESSAGE_TOAST;
                        message.obj = "沒有結案權限！";
                        mHandler.sendMessage(message);

                    }
                } else {
                    Message message = new Message();
                    message.what = Constants.MESSAGE_NETMISTAKE;;
                    message.obj = "網絡問題請求失敗,請重試!";
                    mHandler.sendMessage(message);
                }
//                dismissDialog();
            }
        }.start();
    }

    Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case Constants.MESSAGE_NETMISTAKE://Toast彈出
                    ToastUtils.showLong(YJExceListActivity.this,R.string.net_mistake);
                    break;
                case MESSAGE_TOAST://Toast彈出
                    if (msg.obj.equals("400")) {
                        yjExceMessagesList = new ArrayList<>();
                        dnReformAdapter = new DNReformAdapter(YJExceListActivity.this, yjExceMessagesList,"JQ");
                        swipeListViewOne.setAdapter(dnReformAdapter);
                        ToastUtils.showShort(YJExceListActivity.this, "無異常信息!");

                    } else if (rbEnd.isChecked()){
                        yjExceMessagesList = new ArrayList<>();
                        dnReformAdapter = new DNReformAdapter(YJExceListActivity.this, yjExceMessagesList,"JQ");
                        swipeListViewOne.setAdapter(dnReformAdapter);
                        ToastUtils.showShort(YJExceListActivity.this, msg.obj.toString()
                        );

                    }else{
                        ToastUtils.showShort(YJExceListActivity.this, msg.obj.toString());
                    }
                    break;
                case SUCCESS:
                    getExceList("Y");
                    break;
                case MESSAGE_SEE_ABNORMAL://列表
                    if (rbEnd.isChecked()){
                        dnReformAdapter = new DNReformAdapter(YJExceListActivity.this, yjExceMessagesList,"JQEND");
                    }else{
                    dnReformAdapter = new DNReformAdapter(YJExceListActivity.this, yjExceMessagesList,"JQ");
                    }
                    swipeListViewOne.setAdapter(dnReformAdapter);
                    clickSeeOrAdd();
                    break;
            }
            super.handleMessage(msg);
        }
    };

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId){
            case R.id.rb_all:
                getExceList("N");
                isFirst=false;
                break;
            case R.id.rb_end:
                isZg();
//                getExceList("Y");
                break;
        }
    }

    @Override
    protected void onStart() {

        if (rbAll.isChecked()){
            getExceList("N");
        }else if(rbEnd.isChecked()){
            isZg();
        }else if(isFirst){
            rbAll.setChecked(true);
            isFirst=false;
        }
        super.onStart();
    }

    //    private void aboutAlert(String msg, final int position) {
//        AlertDialog.Builder builder = new AlertDialog.Builder(this);
//        builder.setTitle("提示信息")
//                .setMessage(msg)
//                .setCancelable(false)
//                .setPositiveButton("確認", new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int id) {
//                        // TODO Auto-generated method stub
//                        letGo(yjExceMessagesList.get(position).getYj_id(),FoxContext.getInstance().getName());
//                    }
//                });
//        AlertDialog alert = builder.create();
//        alert.show();
//    }

}
